import tkinter as tk
from tkinter import ttk
import businessmendb


class BusinessmenInputFrame(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.root = master
        self.BusinessmenInput_window = tk.Toplevel(self.master)
        self.BusinessmenInput_window.title('书商信息录入')
        self.BusinessmenInput_window.geometry('%dx%d' % (300, 220))
        self.page = tk.Frame(self.BusinessmenInput_window)
        self.page.pack()
        self.bno = tk.StringVar()
        self.bname = tk.StringVar()
        self.pn = tk.StringVar()
        self.status = tk.StringVar()
        self.create_page()

    def create_page(self):
        tk.Label(self.page, text='书商号: ').grid(row=1, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.bno).grid(row=1, column=1, sticky=tk.E)
        tk.Label(self.page, text='书商名: ').grid(row=2, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.bname).grid(row=2, column=1, sticky=tk.E)
        tk.Label(self.page, text='联系电话: ').grid(row=3, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.pn).grid(row=3, column=1, sticky=tk.E)
        tk.Button(self.page, text='录入书商信息', command=self.record_BusinessmenInput).grid(row=4, column=1, sticky=tk.E,
                                                                                       pady=10)
        tk.Label(self.page, textvariable=self.status).grid(row=5, column=1, sticky=tk.E, pady=10)

    def record_BusinessmenInput(self):
        bno = self.bno.get()
        bname = self.bname.get()
        pn = self.pn.get()

        if not bno:
            self.status.set('录入失败，书商号不能为空')
            return

        if businessmendb.businessman_exists(bno):
            self.status.set('录入失败，书商号已存在')
            return

        businessmendb.insert_businessman(bno, bname, pn)
        self.status.set('书商信息录入成功')



class BusinessmenQueryFrame(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.root = master
        self.BusinessmenQuery_window = tk.Toplevel(self.root)
        self.BusinessmenQuery_window.title('书商信息查询')
        self.BusinessmenQuery_window.geometry('%dx%d' % (300, 220))
        self.page = tk.Frame(self.BusinessmenQuery_window)
        self.page.pack()
        self.bno = tk.StringVar()
        self.bname = tk.StringVar()
        self.pn = tk.StringVar()
        self.status = tk.StringVar()  # 确保初始化 status 变量
        self.create_page()

    def create_page(self):
        tk.Label(self.page, text='书商号: ').grid(row=1, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.bno).grid(row=1, column=1, sticky=tk.E)
        tk.Label(self.page, text='书商名: ').grid(row=2, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.bname, state='readonly').grid(row=2, column=1, sticky=tk.E)
        tk.Label(self.page, text='联系电话: ').grid(row=3, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.pn, state='readonly').grid(row=3, column=1, sticky=tk.E)
        tk.Button(self.page, text='查询书商信息', command=self.query_BusinessmenInfo).grid(row=4, column=1, sticky=tk.E, pady=10)
        tk.Label(self.page, textvariable=self.status).grid(row=5, column=1, sticky=tk.E, pady=10)

    def query_BusinessmenInfo(self):
        bno = self.bno.get()
        result = businessmendb.query_by_bno(bno)
        if result:
            self.bname.set(result['bname'])
            self.pn.set(result['pn'])
            self.status.set('查询成功')
        else:
            self.bname.set('')  # 如果没有找到信息，清空书商名和联系电话输入框
            self.pn.set('')
            self.status.set('没有找到对应的书商信息')


class BusinessmenChangeFrame(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.BusinessmenChange_window = tk.Toplevel(self.master)
        self.BusinessmenChange_window.title('书商信息修改')
        self.BusinessmenChange_window.geometry('%dx%d' % (300, 220))
        self.page = tk.Frame(self.BusinessmenChange_window)
        self.page.pack()
        self.bno = tk.StringVar()
        self.bname = tk.StringVar()
        self.pn = tk.StringVar()
        self.status = tk.StringVar()  # 确保初始化 status 变量
        self.create_page()

    def create_page(self):
        tk.Label(self.page, text='书商号: ').grid(row=1, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.bno).grid(row=1, column=1, sticky=tk.E)
        tk.Label(self.page, text='书商名: ').grid(row=2, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.bname).grid(row=2, column=1, sticky=tk.E)
        tk.Label(self.page, text='联系电话: ').grid(row=3, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.pn).grid(row=3, column=1, sticky=tk.E)
        tk.Button(self.page, text='修改书商信息', command=self.change_BusinessmenInfo).grid(row=4, column=1, sticky=tk.E, pady=10)
        tk.Label(self.page, textvariable=self.status).grid(row=5, column=1, sticky=tk.E, pady=10)

    def change_BusinessmenInfo(self):
        bno = self.bno.get()
        data = {
            'bname': self.bname.get(),
            'pn': self.pn.get()
        }
        result = businessmendb.query_by_bno(bno)
        if result:
            businessmendb.update_by_bno(bno, data)
            self.status.set('修改成功')
        else:
            self.status.set('没有找到对应的书商信息')
